home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / wsgiref / headers.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  7.4 KB  |  184 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Manage HTTP Response Headers
  5.  
  6. Much of this module is red-handedly pilfered from email.message in the stdlib,
  7. so portions are Copyright (C) 2001,2002 Python Software Foundation, and were
  8. written by Barry Warsaw.
  9. '''
  10. from types import ListType, TupleType
  11. import re
  12. tspecials = re.compile('[ \\(\\)<>@,;:\\\\"/\\[\\]\\?=]')
  13.  
  14. def _formatparam(param, value = None, quote = 1):
  15.     '''Convenience function to format and return a key=value pair.
  16.  
  17.     This will quote the value if needed or if quote is true.
  18.     '''
  19.     if value is not None and len(value) > 0:
  20.         if quote or tspecials.search(value):
  21.             value = value.replace('\\', '\\\\').replace('"', '\\"')
  22.             return '%s="%s"' % (param, value)
  23.         return '%s=%s' % (param, value)
  24.     len(value) > 0
  25.     return param
  26.  
  27.  
  28. class Headers:
  29.     '''Manage a collection of HTTP response headers'''
  30.     
  31.     def __init__(self, headers):
  32.         if type(headers) is not ListType:
  33.             raise TypeError('Headers must be a list of name/value tuples')
  34.         type(headers) is not ListType
  35.         self._headers = headers
  36.  
  37.     
  38.     def __len__(self):
  39.         '''Return the total number of headers, including duplicates.'''
  40.         return len(self._headers)
  41.  
  42.     
  43.     def __setitem__(self, name, val):
  44.         '''Set the value of a header.'''
  45.         del self[name]
  46.         self._headers.append((name, val))
  47.  
  48.     
  49.     def __delitem__(self, name):
  50.         '''Delete all occurrences of a header, if present.
  51.  
  52.         Does *not* raise an exception if the header is missing.
  53.         '''
  54.         name = name.lower()
  55.         self._headers[:] = _[1]
  56.  
  57.     
  58.     def __getitem__(self, name):
  59.         """Get the first header value for 'name'
  60.  
  61.         Return None if the header is missing instead of raising an exception.
  62.  
  63.         Note that if the header appeared multiple times, the first exactly which
  64.         occurrance gets returned is undefined.  Use getall() to get all
  65.         the values matching a header field name.
  66.         """
  67.         return self.get(name)
  68.  
  69.     
  70.     def has_key(self, name):
  71.         '''Return true if the message contains the header.'''
  72.         return self.get(name) is not None
  73.  
  74.     __contains__ = has_key
  75.     
  76.     def get_all(self, name):
  77.         '''Return a list of all the values for the named field.
  78.  
  79.         These will be sorted in the order they appeared in the original header
  80.         list or were added to this instance, and may contain duplicates.  Any
  81.         fields deleted and re-inserted are always appended to the header list.
  82.         If no fields exist with the given name, returns an empty list.
  83.         '''
  84.         name = name.lower()
  85.         return _[1]
  86.  
  87.     
  88.     def get(self, name, default = None):
  89.         """Get the first header value for 'name', or return 'default'"""
  90.         name = name.lower()
  91.         for k, v in self._headers:
  92.             if k.lower() == name:
  93.                 return v
  94.         
  95.         return default
  96.  
  97.     
  98.     def keys(self):
  99.         '''Return a list of all the header field names.
  100.  
  101.         These will be sorted in the order they appeared in the original header
  102.         list, or were added to this instance, and may contain duplicates.
  103.         Any fields deleted and re-inserted are always appended to the header
  104.         list.
  105.         '''
  106.         return [ k for k, v in self._headers ]
  107.  
  108.     
  109.     def values(self):
  110.         '''Return a list of all header values.
  111.  
  112.         These will be sorted in the order they appeared in the original header
  113.         list, or were added to this instance, and may contain duplicates.
  114.         Any fields deleted and re-inserted are always appended to the header
  115.         list.
  116.         '''
  117.         return [ v for k, v in self._headers ]
  118.  
  119.     
  120.     def items(self):
  121.         '''Get all the header fields and values.
  122.  
  123.         These will be sorted in the order they were in the original header
  124.         list, or were added to this instance, and may contain duplicates.
  125.         Any fields deleted and re-inserted are always appended to the header
  126.         list.
  127.         '''
  128.         return self._headers[:]
  129.  
  130.     
  131.     def __repr__(self):
  132.         return 'Headers(%r)' % self._headers
  133.  
  134.     
  135.     def __str__(self):
  136.         '''str() returns the formatted headers, complete with end line,
  137.         suitable for direct HTTP transmission.'''
  138.         return []([ '%s: %s' % kv for kv in self._headers ] + [
  139.             '',
  140.             ''])
  141.  
  142.     
  143.     def setdefault(self, name, value):
  144.         """Return first matching header value for 'name', or 'value'
  145.  
  146.         If there is no header named 'name', add a new header with name 'name'
  147.         and value 'value'."""
  148.         result = self.get(name)
  149.         if result is None:
  150.             self._headers.append((name, value))
  151.             return value
  152.         return result
  153.  
  154.     
  155.     def add_header(self, _name, _value, **_params):
  156.         '''Extended header setting.
  157.  
  158.         _name is the header field to add.  keyword arguments can be used to set
  159.         additional parameters for the header field, with underscores converted
  160.         to dashes.  Normally the parameter will be added as key="value" unless
  161.         value is None, in which case only the key will be added.
  162.  
  163.         Example:
  164.  
  165.         h.add_header(\'content-disposition\', \'attachment\', filename=\'bud.gif\')
  166.  
  167.         Note that unlike the corresponding \'email.message\' method, this does
  168.         *not* handle \'(charset, language, value)\' tuples: all values must be
  169.         strings or None.
  170.         '''
  171.         parts = []
  172.         if _value is not None:
  173.             parts.append(_value)
  174.         
  175.         for k, v in _params.items():
  176.             if v is None:
  177.                 parts.append(k.replace('_', '-'))
  178.                 continue
  179.             parts.append(_formatparam(k.replace('_', '-'), v))
  180.         
  181.         self._headers.append((_name, '; '.join(parts)))
  182.  
  183.  
  184.